package org.blogsite.greencode.dao.impl;

import java.util.Iterator;
import java.util.List;

import org.blogsite.greencode.bean.SystemBulletinBoard;
import org.blogsite.greencode.bean.User;
import org.blogsite.greencode.dao.SystemDAO;
import org.jasypt.util.password.BasicPasswordEncryptor;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class SystemDaoImpl extends HibernateDaoSupport implements SystemDAO {
	Iterator iterator;
	private BasicPasswordEncryptor basicPasswordEncryptor;

	public BasicPasswordEncryptor getBasicPasswordEncryptor() {
		return basicPasswordEncryptor;
	}
	public void setBasicPasswordEncryptor(
			BasicPasswordEncryptor basicPasswordEncryptor) {
		this.basicPasswordEncryptor = basicPasswordEncryptor;
	}
	/* 验证注册信息是否正确 */
	public boolean checkRegisterInfo(String name, String job_num) {

		iterator = getHibernateTemplate()
				.find(
						"from InstructorManage_Instructor imi where imi.name=? and imi.job_num=?",
						new String[] { name, job_num }).iterator();
		if (iterator.hasNext()) {
			return true;
		}
		return false;
	}

	/* 注册保存用户信息 */
	public void save(User user) {
		try {
			getHibernateTemplate().save(user);
		} catch (Exception e) {
			System.out.println("注册信息保存失败");
			e.printStackTrace();
		}
	}
	
	/*验证用户名是否可用*/
	public boolean checkUsername(String username){
		try {
			iterator = getHibernateTemplate().find("from User user where user.username =?",username).iterator();
			if (iterator.hasNext()) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return true;
		}
		return false;
	}
	/*验证登录返回该用户对象,然后通过jasypt验证密码正解性*/
	public User checkLogin(User user){
		
		try {
			iterator = getHibernateTemplate().find("from User user where user.username =?",user.getUsername()).iterator();
			if (iterator.hasNext()) {
				return (User)iterator.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return null;
	}
	/*当登录验证成功,修改最后登录与最后IP*/
	public void updateLastDateAndIP(User user){
		try {
			getHibernateTemplate().update(user);
		} catch (Exception e) {
			System.out.println("改最后登录与最后IP-error");
			e.printStackTrace();
		}
	}
	
	/*查询用户个人信息*/
	public User searchPersonInformation(String userName){
		try {
			return (User)getHibernateTemplate().find("from User user where user.username =?",userName).iterator().next();
		} catch (Exception e) {
			System.out.println("查询用户个人信息error");
			e.printStackTrace();
			return null;
		}
		
	}
	/*添加公告板信息*/
	public void addBulletinBoard(SystemBulletinBoard systemBulletinBoard){
		try {
			getHibernateTemplate().save(systemBulletinBoard);
		} catch (Exception e) {
			System.out.println("添加公告板信息error");
			e.printStackTrace();
		}
	}
	
	/*返回用户对象*/
	public User getUser(String name){
		try {
			return (User)getHibernateTemplate().find("from User user where user.username =?",name).iterator().next();
			
		} catch (Exception e) {
			System.out.println("返回用户对象");
			e.printStackTrace();
		return null;
		}
	}
	/*修改用户密码*/
	public void modifyPassword(User user){
		try {
			getHibernateTemplate().update(user);
		} catch (Exception e) {
			System.out.println("密码修改error");
			e.printStackTrace();
		}
		
	}
	/*jasypt开源项目为密码加密,*/
	public String encryptPassword(String password){
		return getBasicPasswordEncryptor().encryptPassword(password);
	}
	/*jasypt检查密码合法性*/
	public boolean checkPassword(String plainPassword,String encryptedPassword){
		return getBasicPasswordEncryptor().checkPassword(plainPassword, encryptedPassword);
	}
	/*显示所有公告*/
	public List displayAllBulletin(){
		try {
			return getHibernateTemplate().find("from SystemBulletinBoard");
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/*查询公告*/
	public void deleteBulletin(SystemBulletinBoard sysBoard){
		try {
			
			getHibernateTemplate().delete(sysBoard);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/*显示公告详细内容*/
	public SystemBulletinBoard showDetailBulletinBoard(int id){
try {
			
			return (SystemBulletinBoard) getHibernateTemplate().find("from SystemBulletinBoard bb where bb.id=?",id).iterator().next();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
